package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannerService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannerServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannerService {

    /**
     * 分页查询
     *
     * @param channelDto
     * @return ResponseResult
     */
    @Override
    public ResponseResult findPage(ChannelDto channelDto) {
        //参数检测
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDto.checkParam();
        //频道名称模糊查询
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();
        if (StringUtils.isNotEmpty(channelDto.getName())) {
            wrapper.like(AdChannel::getName, channelDto.getName());
        }
//        状态码精确查询
        if (ObjectUtils.allNotNull(channelDto.getStatus())) {
            wrapper.eq(AdChannel::getStatus, channelDto.getStatus());
        }
        //添加排序
        wrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> result = page(new Page<AdChannel>(channelDto.getPage(), channelDto.getSize()), wrapper);
        //数据封装返回
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), result.getTotal(),result.getRecords());
        return responseResult;

    }

    /**
     * 新增
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult add(AdChannel adChannel) {
        //参数检测
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isEmpty(adChannel.getName()) || StringUtils.isEmpty(adChannel.getDescription())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断是否默认
        if (adChannel.getIsDefault()) {
            //查询并修改已存在的默认
            LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();
            wrapper.eq(AdChannel::getIsDefault, adChannel.getIsDefault());
            AdChannel one = getOne(wrapper);
            if (one != null) {
                one.setIsDefault(false);
                updateById(one);

            }
        }
        //执行添加
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        //封装返回数据
        return ResponseResult.okResult();
    }

    /**
     * 修改
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //参数检测
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否存在
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否默认
        if (adChannel.getIsDefault()) {
            //存在默认,则查询并修改已存在的默认
            LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();
            wrapper.eq(AdChannel::getIsDefault, adChannel.getIsDefault());
            AdChannel one = getOne(wrapper);
            if (one != null) {
                if (adChannel.getId().intValue() != one.getId().intValue()) {
                    one.setIsDefault(false);//修改成非默认
                    updateById(one);
                }
            }
        }
        //执行更新
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //参数检测"
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断是否存在
        AdChannel channel = getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        int i = 1/0;
        //启动状态无法删除
        if (channel.getStatus()) {
           CustException.cust(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //执行删除
        removeById(id);
        return ResponseResult.okResult();
    }
}
